package com.smallkingson.ylib.view;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnticipateOvershootInterpolator;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smallkingson.ylib.core.Yr;
import com.smallkingson.ylib.view.utils.YViews;

import java.util.HashMap;
import java.util.Map;

@SuppressLint("NewApi")
public class YPopupWindow {

    public PopupWindow popupWindow;

    private RelativeLayout popupView;

    private View containerView;

    private View fgView;

    private Context context;

    private View bgView;

    private Map<String, Object> tags;

    private Object tag;

    private OnStateChangeListener onStateChangeListener = new OnStateChangeListener() {

        @Override
        public boolean onShow(YPopupWindow popupWindow, View fgView, View bgView) {
            return false;
        }

        @Override
        public boolean onDismiss(YPopupWindow popupWindow, View fgView,
                                 View bgView) {
            return false;
        }
    };

    private YPopupWindow(RelativeLayout popupView, View containerView) {

        this.popupView = popupView;
        this.containerView = containerView;
    }

    private YPopupWindow(Context context) {
        this.context = context;
    }

    public YPopupWindow(Activity activity,View popupView,int width,int height) {

        this(popupView,YViews.getContentView(activity),width,height);
    }
    public YPopupWindow(Activity activity,int layoutRes,int width,int height) {

        this(LayoutInflater.from(activity.getApplicationContext()).inflate(layoutRes,null),YViews.getContentView
                (activity),width,height);
    }
    public YPopupWindow(View popupView,
                        View showInTheView,int width,int height) {
        this.context = popupView.getContext();
        this.popupView = new RelativeLayout(context);

        tags = new HashMap<String, Object>();

        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(width, height);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        this.popupView.addView(popupView, lp);

        this.containerView = showInTheView;

        popupWindow = new PopupWindow(this.popupView, ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT, true);

        popupWindow.setFocusable(true);
        //popupView.setFocusable(true);
        //popupView.setFocusableInTouchMode(true);
        popupView.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (event.getAction() == KeyEvent.ACTION_DOWN
                        && keyCode == KeyEvent.KEYCODE_BACK)
                    dismiss();
                return false;
            }
        });

        this.fgView = popupView;
        this.fgView.setVisibility(View.GONE);
        addBgView();
    }


    private void addBgView() {
        setBgView(new View(context));
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        getBgView().setClickable(true);
        getBgView().setBackgroundColor(0x33000000);
        getBgView().setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                dismiss();
            }
        });
        popupView.addView(getBgView(), 0, lp);
    }

    public void show() {
        containerView.post(new Runnable() {
            @Override public void run() {
                try {
                    popupWindow.showAtLocation(containerView, Gravity.CENTER, 0, 0);
                    fgView.setVisibility(View.VISIBLE);
                    if (getOnStateChangeListener().onShow(YPopupWindow.this, fgView, getBgView())) {
                        return;
                    }
                    showFgView(fgView);
                } catch (Exception e) {
                    Yr.logError(e);
                }
            }
        });
    }

    protected void showFgView(final View fgView) {

        ValueAnimator animator = ValueAnimator.ofFloat(0.0f, 1.0f).setDuration(
                500);

        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {

                float animatedFraction = (Float) animation.getAnimatedValue();

                fgView.setAlpha(animatedFraction);
            }
        });
        animator.start();
    }

    public void dismiss() {

        if (onStateChangeListener.onDismiss(this, this.fgView, getBgView())) {
            return;
        }
        performDismiss();
    }

    public void performDismiss() {
        this.fgView.setVisibility(View.GONE);
        popupWindow.dismiss();

    }

    public boolean isShowing() {
        return popupWindow.isShowing();
    }

    public YPopupWindow setOnClickListener(int id, View.OnClickListener clickListener) {
        popupView.findViewById(id).setOnClickListener(clickListener);
        return this;
    }

    public View findViewById(int id) {
        return popupView.findViewById(id);
    }

    public String getText(int id) {

        TextView textView = (TextView) popupView.findViewById(id);

        return textView.getText().toString();
    }

    /**
     * @return the onStateChangeListener
     */
    public OnStateChangeListener getOnStateChangeListener() {
        return onStateChangeListener;
    }

    /**
     * @param onStateChangeListener the onStateChangeListener to set
     */
    public void setOnStateChangeListener(
            OnStateChangeListener onStateChangeListener) {
        this.onStateChangeListener = onStateChangeListener;
    }

    /**
     * @return the tags
     */
    public Object getTag(String key) {
        return tags.get(key);
    }

    /**
     * the tags to set
     */
    public void setTags(String key, Object value) {
        tags.put(key, value);
    }

    /**
     * @return the bgView
     */
    public View getBgView() {
        return bgView;
    }

    /**
     * @param bgView the bgView to set
     */
    private void setBgView(View bgView) {
        this.bgView = bgView;
    }


    /**
     * @return the tag
     */
    public Object getTag() {
        return tag;
    }

    /**
     * @param tag the tag to set
     */
    public void setTag(Object tag) {
        this.tag = tag;
    }

    public View getFgView() {
        return fgView;
    }

    public static interface OnStateChangeListener {

        boolean onShow(YPopupWindow popupWindow, View fgView, View bgView);

        boolean onDismiss(YPopupWindow popupWindow, View fgView, View bgView);
    }
}